//
//  CLTabBarController.m
//  CLBrowser
//
//  Created by chuck on 2021/7/8.
//

#import "CLTabBarController.h"
#import "CLTabBar.h"

#import "CLNavigationController.h"
#import "CLVCBookmark.h"
#import "CLVCMine.h"
#import "CLVCSearchBook.h"
#import "CLVCBookSheif.h"
@interface CLTabBarController ()
/// tabBarController
@property (nonatomic, strong, readwrite) UITabBarController *tabBarController;
@end

@implementation CLTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    WEAK_SELF;
    if (self.navigationController) {
        
    }
    self.tabBarController = [[UITabBarController alloc] init];
    [self.view addSubview:self.tabBarController.view];
    [self addChildViewController:self.tabBarController];
    [self.tabBarController didMoveToParentViewController:self];
//    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    self.tabBarController.delegate = self;
    CLTabBar *tabBar = [[CLTabBar alloc] init];
    [self.tabBarController setValue:tabBar forKeyPath:@"tabBar"];
    [self.tabBarController addChildViewController:[self addChildVCWithVC:[CLVCBookSheif new] Title:@"首页" Image:@"home_notselect" SelectImage:@"home_select"]];
    [self.tabBarController addChildViewController:[self addChildVCWithVC:[CLVCMine new] Title:@"我的" Image:@"mine_notselect" SelectImage:@"mine_select"]];
    
    tabBar.centerBtnClick = ^{
        CLVCSearchBook *vc = [[CLVCSearchBook alloc] init];
        [CLNavigationService pushController:vc animated:YES];
    };
    
    [RACObserve(self.tabBarController.tabBar, frame) subscribeNext:^(id x) {
        dispatch_async(dispatch_get_main_queue(), ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            if ([UIApplication sharedApplication].statusBarFrame.size.height == 40) {
#pragma clang diagnostic pop
                weakSelf.tabBarController.tabBar.frame = CGRectMake(0, SCREENHEIGHT - weakSelf.tabBarController.tabBar.frame.size.height - 20, weakSelf.tabBarController.tabBar.frame.size.width, weakSelf.tabBarController.tabBar.frame.size.height);
            } else {
                weakSelf.tabBarController.tabBar.frame = CGRectMake(0, SCREENHEIGHT - weakSelf.tabBarController.tabBar.frame.size.height, weakSelf.tabBarController.tabBar.frame.size.width, weakSelf.tabBarController.tabBar.frame.size.height);
            }
        });
    }];
}
- (void)viewWillLayoutSubviews {
    self.tabBarController.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    self.fd_interactivePopDisabled = YES;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    if ([UIApplication sharedApplication].statusBarFrame.size.height == 40) {
#pragma clang diagnostic pop
        self.tabBarController.tabBar.frame = CGRectMake(0, SCREENHEIGHT - self.tabBarController.tabBar.frame.size.height - 20, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
    } else {
        self.tabBarController.tabBar.frame = CGRectMake(0, SCREENHEIGHT - self.tabBarController.tabBar.frame.size.height, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
    }
}

- (CLNavigationController *)addChildVCWithVC:(UIViewController *)childVC Title:(NSString *)title Image:(NSString *)imgName SelectImage:(NSString *)selectImgName {
    childVC.title = title;
    childVC.tabBarItem.title = title;
    childVC.tabBarItem.image = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(exit)];
    childVC.navigationItem.rightBarButtonItem = cancelItem;
    
    CLNavigationController *nvc = [[CLNavigationController alloc] initWithRootViewController:childVC];
    
    return nvc;
}
- (void)exit {
    [CLNavigationService popToRootControllerAnimated:YES];
}
#pragma mark - Ovveride
- (BOOL)shouldAutorotate {
    return self.tabBarController.selectedViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.tabBarController.selectedViewController.supportedInterfaceOrientations;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.tabBarController.selectedViewController.preferredStatusBarStyle;
}

- (BOOL)prefersStatusBarHidden {
    return self.tabBarController.selectedViewController.prefersStatusBarHidden;
}
- (BOOL)isHiddenNaviBar {
    return YES;
}
@end
